﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Db;
using System.Data;

namespace Db.Tool
{
    internal class SQLTool : ITool
    {
        public Dictionary<string, string> GetTableList(DbHelper hp)
        {
            Dictionary<string, string> tableDic = new Dictionary<string, string>();
            
            //获取表
            const string sql = "Select [Name] FROM SysObjects Where XType='U' ";
            var comm = hp.GetSqlStringCommond(sql);
            DataTable dt = hp.ExecuteDataTable(comm);

            for (int i = 0; i < dt.Rows.Count; i++)
            {
                string table = dt.Rows[i]["Name"].ToString();

                //获取主键
                var com = hp.GetSqlStringCommond("exec sp_helpconstraint '" + table + "' ;");
                var tableKey = hp.ExecuteScalar(com);

                if (tableKey == null)
                {
                    throw new Exception(table + "表没有主键");
                }

                tableDic.Add(table, tableKey.ToString());
            }

            return tableDic;
        }
    }
}
